// pages/login/login.js
import ajax from '../../utils/index'
const app = getApp()
Page({

  /**
   * 页面的初始数据
   */
  data: {
    /**
     * login code
     */
    loginCode: null,
    /**
     * 分享id
     */
    shareId: null,
    /**
     * 服务协议
     */
    agreement: false,
    /**
     * 标题栏高度
     */
    headlineHeight: 0,
    /**
     * 绑定手机弹出框 状态
     */
    showMode: false,
    /**
     * 用户对象
     */
    userInfo: null
  },
  /**
   * 关闭绑定手机号弹窗
   */
  modeCancel() {
    this.setData({ showMode: false })
  },
  backtrack() {
    wx.switchTab({
      url: '/pages/index/index',
    })
  },
  /**
   * 服务协议点击事件
   */
  agreement_change(event) {
    this.setData({ agreement: event.detail })
  },
  /**
   * 获取登录code
   */
  async getLoginCode() {
    this.modeCancel()
    const res = await ajax.getLoginCode()
    this.data.loginCode = res.code
  },
  /**
   * 登录事件
   */
  async loginEvent() {
    const { agreement, shareId } = this.data;
    if (!agreement) return ajax.showToast('请同意并阅读盈联易单配用户服务协议');
    ajax.showLoading()
    const wxUserInfo = await ajax.getUserPro().finally(() => wx.hideLoading());
    if (wxUserInfo.errMsg != 'getUserProfile:ok') return;
    wxUserInfo.code = this.data.loginCode, wxUserInfo.shareId = shareId, ajax.showLoading();
    const res = await ajax.post('/wx/user/login', wxUserInfo).finally(() => wx.hideLoading());
    const { phone, user, token } = res.data;
    if (!phone) return this.setData({ userInfo: user, showMode: true });
    wx.setStorageSync('Token', token), wx.setStorageSync('userInfo', user);
    await ajax.showSuccess('登录成功');
    wx.setStorageSync('pageRoles', 0)
    wx.navigateBack();
  },
  /**
   * 获取手机号信息, 用户信息
   */
  async getPhoneNumber(event) {
    if (event.detail.errMsg !== 'getPhoneNumber:ok') return
    ajax.showLoading();
    const entity = event.detail, { loginCode, userInfo } = this.data;
    entity.code = loginCode, entity.userId = userInfo.id;
    const res = await ajax.post('/wx/user/phone', entity).finally(() => wx.hideLoading());
    const { user, token } = res.data;
    wx.setStorageSync('Token', token), wx.setStorageSync('userInfo', user);
    await ajax.showSuccess('登录成功');
    wx.setStorageSync('pageRoles', 0)
    wx.navigateBack();
  },
  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    const headlineHeight = ajax.getHeadlineHeight()
    this.setData({ headlineHeight })
    // 分享人id
    this.data.shareId = options.shareId || app.globalData.shareId || ''
    // wx.removeStorageSync('Token')
    // wx.removeStorageSync('userInfo')
    wx.setStorageSync('Token', '')
    wx.setStorageSync('userInfo', null)
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {

  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {

  }
})